package com.xdf.ecommerce.partition;

import com.alibaba.fastjson.JSON;
import com.xdf.ecommerce.vo.SCSMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * 自定义分区策略（消息的发送方分区策略）的 key 提取类
 * @author chanchaw
 * @create 2025-11-11 9:56
 */
@Slf4j
@Component
public class QinyiPartitionKeyExtractorStrategy implements PartitionKeyExtractorStrategy {
    @Override
    public Object extractKey(Message<?> message) {
        SCSMessage scsMessage = JSON.parseObject(message.getPayload().toString(), SCSMessage.class);
        String projectName = scsMessage.getProjectName();
        log.info("为发送消息自定义分区策略提取key时得到的 projectName:[{}]", projectName);
        return projectName;
    }
}
